//
//  GHSuggestViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/16.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "LGSuggestViewController.h"
#import "STTextView.h"
#import "UITextField+STInputLimit.h"
#define MAX_NUM 200
@interface LGSuggestViewController ()
@property(nonatomic, strong) STTextView                     *textView;
@property(nonatomic, strong) UITextField                    *textFiled;
@property(nonatomic, strong) STLabel                        *numLable;
@end

@implementation LGSuggestViewController
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"意见反馈";
    //[self st_setRightItemWithTitle:@"保存" titleColor:[UIColor whiteColor]];
    [self configSubView];
    [self addNotifacations];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --Notifacation
- (void)addNotifacations{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(UITextFieldTextDidChange:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(UITextViewTextDidChange:)
                                                 name:UITextViewTextDidChangeNotification
                                               object:nil];
}
- (void)UITextFieldTextDidChange:(NSNotification*)notify{
    if (notify.object == self.textFiled) {
        [self.textFiled st_textInputLimitWithLength:11];
    }
    
}
- (void)UITextViewTextDidChange:(NSNotification*)notify{
    if (notify.object == self.textView) {
        UITextView *textView = (UITextView *)notify.object;
        NSString *toBeString = textView.text;
        NSString *lang = [[UIApplication sharedApplication] textInputMode].primaryLanguage; // 键盘输入模式
        if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，五笔，简体手写
            UITextRange *selectedRange = [textView markedTextRange];
            //获取高亮部分
            UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
            // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
            if (!position) {
                if (toBeString.length > MAX_NUM) {
                    textView.text = [toBeString substringToIndex:MAX_NUM];
                }
                NSInteger num = self.textView.text.length;
                self.numLable.text = [NSString stringWithFormat:@"%ld/200", num];
            }
            // 有高亮选择的字符串，则暂不对文字进行统计和限制
            else{
                
            }
        }
        // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
        else{
            NSInteger num = toBeString.length;
            self.numLable.text = [NSString stringWithFormat:@"%ld/200",num];
            if (toBeString.length >= MAX_NUM) {
                textView.text = [toBeString substringToIndex:MAX_NUM];
            }
        }
    }
}
- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark --subView
- (void)configSubView{
    [self configTableHeader];
    [self configTableFooter];
}
- (void)configTableHeader{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    header.backgroundColor =  self.tableView.backgroundColor;
    
    UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, UIScreenWidth, 180)];
    whiteView.backgroundColor =  [UIColor whiteColor];
    [header addSubview:whiteView];
    self.textView = [[STTextView alloc] initWithFrame:CGRectMake(16, 16, UIScreenWidth - 32, 144)];
    self.textView.placeholder = @"请输入反馈信息，200字以内";
    self.textView.placeholderColor = SecendTextColor;
    [whiteView addSubview:self.textView];
    
    self.numLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)
                                              text:@"0/200"
                                         textColor:SecendTextColor
                                              font:12
                                       isSizetoFit:NO
                                     textAlignment:NSTextAlignmentRight];
    self.numLable.st_right = UIScreenWidth - 16;
    self.numLable.st_bottom = whiteView.st_height - 14;
    [whiteView addSubview:self.numLable];
    
    UIView * textFiledBackView = [[UIView alloc] initWithFrame:CGRectMake(0, whiteView.st_bottom + 10   , UIScreenWidth, 44)];
    textFiledBackView.backgroundColor =  [UIColor whiteColor];
    [header addSubview:textFiledBackView];
    
    self.textFiled = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, UIScreenWidth - 20, 44)];
    self.textFiled.keyboardType = UIKeyboardTypeNumberPad;
    self.textFiled.placeholder = @"请输入你的手机号";
    self.textFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    [textFiledBackView addSubview:self.textFiled];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(10, textFiledBackView.st_bottom + 10, UIScreenWidth - 20, 14)
                                                     text:@"您的联系方式有助于我们沟通和解决问题。仅工作人员可见"
                                                textColor:SecendTextColor
                                                     font:13
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [header addSubview:titleLable];
    header.st_height = titleLable.st_bottom + 65;
    
    self.tableView.tableHeaderView = header;
}
- (void)configTableFooter{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60, 44)
                                                        title:@"发送反馈"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:5
                                              backgroundColor:TM_redColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCommitButton];
    }];
    confimButton.st_centerY = 50;
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    self.tableView.tableFooterView = footer;
}
#pragma mark --Action Method
- (void)onSelectedCommitButton{
    if (!self.textView.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入您的建议"];
        return;
    }
    if (!self.textFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入您的联系方式"];
        return;
    }
    [self sendFeadbacekRequest];
}
#pragma mark --NetWork Method
- (void)sendFeadbacekRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/feedbackAction"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if (self.textView.text.length) {
        [dic setObject:self.textView.text forKey:@"content"];
    }
    if (self.textFiled.text.length) {
        [dic setObject:self.textFiled.text forKey:@"mobile"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"感谢你的建议"];
                                                 [self.navigationController popViewControllerAnimated:YES];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end

